<?php
namespace Admin\Controller;
use Think\Controller;
class ServicechargeController extends BaseController{
    public function catefee(){
        $m=M('Feecate');
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();

		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	//添加
	function add_catefees(){
			$m=M('Feecate');
			$param = I('param.','','trim');
			$param['add_time']=time();
			$res=$m->add($param);
			if($res){
				$this->success('添加成功');
			}
	}
	//更新
	function add_catefee(){
		if(IS_POST){
			$m=M('Feecate');
			$param = I('param.','','trim');
			$param['update_time'] = time();
			$id=$param['id'];
			$res=$m->where(array('id'=>$id))->save($param);
			
			if($res!==false){
				$this->success('更新成功');
			}
		}else{
			$this->display();
		}
	}
	//编辑
	function edit_catefee(){
		$id=trim(I('id'));
		$m=M('Feecate');
		$detail=$m->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	function del_catefee(){
		$id=trim(I('id'));
		$m=M('Feecate');
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	
	public function feelist(){
        $m=M('Feelist');
		
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();

		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		$n=M('Feecate');
		foreach($list as $k=>$v){
			$feecate=$n->where(array('id'=>$v['feecate']))->find();
			$list[$k]['feecate_name']=$feecate['name'].'('.$feecate['cate'].')';
		}
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	//添加
	function add_feelists(){
			$m=M('Feelist');
			$param = I('param.','','trim');
			$param['add_time']=time();
			$res=$m->add($param);
			if($res){
				$this->success('添加成功');
			}
	}
	//更新
	function add_feelist(){
		$n=M('Feecate');
		$feecate_list=$n->where(array('status'=>1))->select();
		
		$this->assign("feecate_list",$feecate_list);
		if(IS_POST){
			$m=M('Feelist');
			$param = I('param.','','trim');
			$param['update_time'] = time();
			$id=$param['id'];
			$res=$m->where(array('id'=>$id))->save($param);
			
			if($res!==false){
				$this->success('更新成功');
			}
		}else{
			$this->display();
		}
	}
	//编辑
	function edit_feelist(){
		$n=M('Feecate');
		$feecate_list=$n->where(array('status'=>1))->select();
		
		$this->assign("feecate_list",$feecate_list);
		$id=trim(I('id'));
		$m=M('Feelist');
		$detail=$m->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	function del_feelist(){
		$id=trim(I('id'));
		$m=M('Feelist');
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	
	
	public function feerule(){
        $m=M('Feerule');
		
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();

		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		$n=M('Feelist');
		foreach($list as $k=>$v){
			$list_cates=explode(':',$v['group_id']);
			$list_cate=explode(',',$list_cates[0]);
			foreach($list_cate as $ks=>$vs){
				$info=$n->where(array('id'=>$vs))->find();
				if(!$ks){
					$list[$k]['feelist_name'].=$info['name'];
				}else{
					$list[$k]['feelist_name'].=' | '.$info['name'];
				}
				
			}
		}
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	//添加
	function add_feerules(){
		
			$m=M('Feerule');
			$param = I('param.','','trim');
			$param['add_time']=time();
			$res=$m->add($param);
			if($res){
				$this->success('添加成功');
			}
	}
	//更新
	function add_feerule(){
		$cate_list=M('Feecate')->order("add_time")->select();
		$feelist=M('Feelist')->order("add_time")->select();

		foreach($cate_list as $k=>$v){
			foreach($feelist as $ks=>$vs){
				if($v['id']==$vs['feecate']){
					$cate_list[$k]['children'][]=$vs;
				}
			}
        }
		// dump($new_arr);
		// die;
		$this->assign('list',$cate_list);
		if(IS_POST){
			$m=M('Feerule');
			$param = I('param.','','trim');
			$param['update_time'] = time();
			$id=$param['id'];
			$res=$m->where(array('id'=>$id))->save($param);

			if($res!==false){
				$this->success('更新成功');
			}
		}else{
			$this->display();
		}
	}
	//编辑
	function edit_feerule(){
		$id=trim(I('id'));
		$m=M('Feerule');
		$detail=$m->where(array('id'=>$id))->find();
		$group_arr=explode(':',$detail['group_id']);
		$group_idlist=explode(',',$group_arr[0]);
		$price_arr_idlist=explode(',',$group_arr[1]);
		$unit_arr_idlist=explode(',',$group_arr[2]);

		$cate_list=M('Feecate')->order("add_time")->select();
		$feelist=M('Feelist')->order("add_time")->select();

		foreach($cate_list as $k=>$v){
			foreach($feelist as $ks=>$vs){
				foreach($group_idlist as $key=>$value){
					if($vs['id']==$value){
						$vs['checked']=1;
						$vs['price_arr']=$price_arr_idlist[$key];
						$vs['unit_arr']=$unit_arr_idlist[$key];
					}
				}
				if($v['id']==$vs['feecate']){
					$cate_list[$k]['children'][]=$vs;
				}
			}
        }
		// dump($new_arr);
		// die;
		$this->assign('list',$cate_list);
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	function del_feerule(){
		$id=trim(I('id'));
		$m=M('Feerule');
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	
	public function feelist_other(){
        $m=M('FeelistOther');
		
		$keywords=I('get.keywords');     
		$selval=I('get.selval');     
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($keywords)){
			if($selval=='1'){
				$map['name'] = array('like','%'.$keywords.'%');
			}
        }
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();

		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		$n=M('Feecate');
		foreach($list as $k=>$v){
			$feecate=$n->where(array('id'=>$v['feecate']))->find();
			$list[$k]['feecate_name']=$feecate['name'].'('.$feecate['cate'].')';
		}
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	//添加
	function add_feelists_other(){
			$m=M('FeelistOther');
			$param = I('param.','','trim');
			$param['add_time']=time();
			$res=$m->add($param);
			if($res){
				$this->success('添加成功');
			}
	}
	//更新
	function add_feelist_other(){
		$n=M('Feecate');
		$feecate_list=$n->where(array('status'=>1))->select();
		
		$this->assign("feecate_list",$feecate_list);
		if(IS_POST){
			$m=M('FeelistOther');
			$param = I('param.','','trim');
			$param['update_time'] = time();
			$id=$param['id'];
			$res=$m->where(array('id'=>$id))->save($param);
			
			if($res!==false){
				$this->success('更新成功');
			}
		}else{
			$this->display();
		}
	}
	//编辑
	function edit_feelist_other(){
		$n=M('Feecate');
		$feecate_list=$n->where(array('status'=>1))->select();
		
		$this->assign("feecate_list",$feecate_list);
		$id=trim(I('id'));
		$m=M('FeelistOther');
		$detail=$m->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	function del_feelist_other(){
		$id=trim(I('id'));
		$m=M('FeelistOther');
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	
	public function feelog(){
        $m=M('Feelog');
		
		$feecate_id=I('get.feecate_id');     
		$feelist_id=I('get.feelist_id');  
		$member_id=I('get.member_id');  
		$start_time=I('start_time');
		$end_time=I('end_time');
   
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
        if(!empty($feecate_id)){
			$map['feecate_id'] = $feecate_id;
        }
		if(!empty($feelist_id)){
			$map['feelist_id'] = $feelist_id;
        }
		if(!empty($member_id)){
			$map['member_id'] = $member_id;
        }
		
		if($start_time!='' && $end_time==''){
			$start_time=strtotime($start_time);
			$end_time=strtotime(date('Y-m-d',time()+86400));
        }else if($start_time=='' && $end_time!=''){
			$end_time=strtotime($end_time);
			$start_time=strtotime(date('Y-m-d',time()));
        }else if($start_time=='' && $end_time=='') {
			$start_time=strtotime(date('Y-m-d',time()));
			$end_time=strtotime(date('Y-m-d',time()+86400));
		}else if($start_time!='' && $end_time!='') {
			$start_time=strtotime($start_time);
			$end_time=strtotime($end_time);
		}

		$map['add_time']= array(array('gt',$start_time),array('lt',$end_time));
		
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$total_price=$m->field('sum(count_price) total_count_price')->where($map)->limit($firstrow.','.$listrows)->select();
// dump($total_price);
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		
		$n=M('Feecate');
		$l=M('Feelist');
		$lo=M('FeelistOther');
		$r=M('Feerule');
		$Member=M('Member');
		
		$feecate_list=$n->where(array('status'=>1))->select();
		$feelist_list=$l->where(array('status'=>1))->select();
		$Member_list=$Member->where(array('status'=>1))->select();
		$this->assign('feecate_list',$feecate_list);
		$this->assign('feelist_list',$feelist_list);
		$this->assign('Member_list',$Member_list);
		
		$this->assign('total_price',$total_price[0]['total_count_price']);//合计价格
		foreach($list as $k=>$v){
			$feecate_info=$n->where(array('id'=>$v['feecate_id']))->find();
			$list[$k]['feecate_name']=$feecate_info['name'].'('.$feecate_info['cate'].')';
			if($v['cate']==1){
				$feelist_info=$l->where(array('id'=>$v['feelist_id']))->find();
			}else{
				$feelist_info=$lo->where(array('id'=>$v['feelist_id']))->find();
			}
			$list[$k]['feelist_name']=$feelist_info['name'];
			
			$feerule_info=$r->where(array('id'=>$v['feerule_id']))->find();
			$list[$k]['feerule_name']=$feerule_info['name'];
			
			$member_info=$Member->where(array('id'=>$v['member_id']))->find();
			$list[$k]['member_name']=$member_info['name'];
		}
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	//添加
	function add_feelogs(){
			$Member=M('Member');
			$m=M('Feelog');
			$n=M('FeelistOther');
			$f=M('Feecate');
			$param = I('param.','','trim');
			$param['add_time']=time();
			$param['cate']=2;//手动扣费
			
			$feelist_info=$n->where(array('id'=>$param['feelist_id']))->find();
			$feecate_info=$f->where(array('id'=>$feelist_info['feecate']))->find();
			
			$param['feecate_id']=$feelist_info['feecate'];
			$param['cate_id']=1;//元/单
			$param['out_money_nums']=1;
			
			//扣费
			$outbound_money=$param['count_price'];
			$member_info=$Member->where(array('id'=>$param['member_id']))->find();
			if($member_info['money']-$member_info['out_money']-$outbound_money>0){//余额大于要扣费金额
				$data_member['out_money']=array('exp','out_money+'.(int)$outbound_money);
				$res_member=$Member->where(array('id'=>$param['member_id']))->save($data_member);
				if($res_member===false){
					$this->error('扣费失败！');
					die;
				}
			}
			
			$res=$m->add($param);
			//echo $m->getlastsql();
			if($res){
				$this->success('添加成功');
			}
	}
	//更新
	function add_feelog(){
		$n=M('FeelistOther');
		$f=M('Feecate');
		$Member=M('Member');
		$member_list=$Member->where(array('status'=>1))->select();
		$feelist_other=$n->where(array('status'=>1))->select();
		foreach($feelist_other as $k=>$v){
			$feecate_info=$f->where(array('id'=>$v['feecate']))->find();
			
			$feelist_other[$k]['feecate_name']=$feecate_info['name'];
			$feelist_other[$k]['feecate_cate']=$feecate_info['cate'];
		}
		// dump($feelist_other);
		$this->assign('feelist_other',$feelist_other);
		$this->assign('member_list',$member_list);
		if(IS_POST){
			$m=M('Feelog');
			$param = I('param.','','trim');
			$param['update_time'] = time();
			$id=$param['id'];
			$res=$m->where(array('id'=>$id))->save($param);
			
			if($res!==false){
				$this->success('更新成功');
			}
		}else{
			$this->display();
		}
	}
	//编辑
	function edit_feelog(){
		$n=M('FeelistOther');
		$f=M('Feecate');
		$Member=M('Member');
		$member_list=$Member->where(array('status'=>1))->select();
		$feelist_other=$n->where(array('status'=>1))->select();
		foreach($feelist_other as $k=>$v){
			$feecate_info=$f->where(array('id'=>$v['feecate']))->find();
			
			$feelist_other[$k]['feecate_name']=$feecate_info['name'];
			$feelist_other[$k]['feecate_cate']=$feecate_info['cate'];
		}
		// dump($feelist_other);
		$this->assign('feelist_other',$feelist_other);
		$this->assign('member_list',$member_list);
		
		$id=trim(I('id'));
		$m=M('Feelog');
		$detail=$m->where(array('id'=>$id))->find();
		$this->assign('detail',$detail);
		$this->display();
	}
	//删除
	function del_feelog(){
		$id=trim(I('id'));
		$m=M('Feelog');
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
}